﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class SettingProvider:DataAccess
    {
        public SettingProvider()
        {
            this.CacheDuration = Globals.Settings.Setting.CacheDuration;
            this.EnableCaching = Globals.Settings.Setting.EnableCaching;
            this.ConnectionString = Globals.Settings.ConnectionString;
        }

        #region IDataReader --> Object Settings
        protected virtual SettingsDetails GetSettingFromReader(IDataReader reader)
        {
            return new SettingsDetails(
                (int)reader["Id"],
                (string)reader["Name"],
                (string)reader["Value"]);
        }

        protected virtual List<SettingsDetails> GetSettingCollectionFromReader(IDataReader reader)
        {
            List<SettingsDetails> setting = new List<SettingsDetails>();
            while (reader.Read())
                setting.Add(GetSettingFromReader(reader));
            return setting;
        }

        #endregion

        #region Settings Method

        public List<SettingsDetails> ListSetting()
        {
            using (SqlConnection cn = new SqlConnection(this.ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("SettingList", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cn.Open();
                return GetSettingCollectionFromReader(ExecuteReader(cmd));
            }
        }

        public SettingsDetails SettingGet(int Id)
        {
            using (SqlConnection cn = new SqlConnection(this.ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("SettingsGet", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@Id", SqlDbType.Int).Value = Id;
                cn.Open();
                IDataReader reader = ExecuteReader(cmd, CommandBehavior.SingleRow);
                if (reader.Read())
                    return GetSettingFromReader(reader);
                else
                    return null;
            }
        }

        public String SettingGetValue(string name)
        {
            using (SqlConnection cn = new SqlConnection(this.ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("SettingGetValue_By_Name", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = name;
                cn.Open();
                IDataReader reader = ExecuteReader(cmd, CommandBehavior.SingleRow);
                if (reader.Read())
                    return reader.GetValue(0).ToString();
                else
                    return null;
            }
        }

        public bool SettingUpdate(SettingsDetails setting)
        {
            using (SqlConnection cn = new SqlConnection(this.ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("SettingsUpdate", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = setting.Name;
                cmd.Parameters.Add("@Value", SqlDbType.NVarChar).Value = setting.Value;
                cn.Open();
                int ret = ExecuteNonQuery(cmd);
                return (ret == 1);
            }
        }

        #endregion
    }
}
